package manager;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import user.NotificationBean;
import user.RequestResourceBean;
import utils.DomainDAORemote;
import utils.MeasureunitDAORemote;
import utils.ResourceDAORemote;
import utils.ResponseDAORemote;
import utils.ResticketDAORemote;
import utils.TechresourceDAORemote;
import utils.TechticketDAORemote;
import utils.UserDAORemote;
import beans.ManagerPageBean;
import dtos.DomainDTO;
import dtos.MeasureunitDTO;
import dtos.ResourceDTO;
import dtos.ResponseDTO;
import dtos.ResticketDTO;
import dtos.TechresourceDTO;
import dtos.TechticketDTO;
import dtos.UserDTO;

@ManagedBean(name = "tb")
@SessionScoped
public class TicketBean implements Serializable {
	private static final long serialVersionUID = -6625353284053378621L;

	@EJB
	ResticketDAORemote rtr;

	@EJB
	TechticketDAORemote ttr;

	@EJB
	ResponseDAORemote rr;

	@EJB
	ResourceDAORemote rdr;

	@EJB
	UserDAORemote ur;

	@EJB
	TechresourceDAORemote trr;

	@EJB
	DomainDAORemote dr;

	@EJB
	MeasureunitDAORemote mudr;

	@ManagedProperty(value = "#{managerPageBean}")
	ManagerPageBean mpb;


	private String[] ticketFrom = { "-none-", "Basic user", "Tech guy" };
	private List<ResticketDTO> resTickets;
	private List<TechticketDTO> techTickets;
	private boolean fromUser;
	private boolean fromTech;
	private List<UserDTO> users;
	private ResticketDTO resT;
	private TechticketDTO techT;
	private boolean var1;
	private boolean var2;
	String response;
	UserDTO manager;

	@PostConstruct
	public void init() {
		manager = (UserDTO) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("loggedUser");
		if (manager.getIdDomain() == 1) {
			techTickets = ttr.getAllTechtickets();
			resTickets = rtr.getAllRestickets();
		} else {
			techTickets = ttr.getTechticketsByUserDomain(manager);
			resTickets = rtr.getResticketsByUserDomain(manager);
		}
		var1 = false;
		var2 = false;
		users = ur.getAllUsersDTO();
	}

	public UserDTO getManager() {
		manager = (UserDTO) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("loggedUser");
		return manager;
	}

	public void setManager(UserDTO manager) {
		this.manager = manager;
	}

	public ManagerPageBean getMpb() {
		return mpb;
	}

	public void setMpb(ManagerPageBean mpb) {
		this.mpb = mpb;
	}

	public List<ResticketDTO> getResTickets() {
		return resTickets;
	}

	public void setResTickets(List<ResticketDTO> resTickets) {
		this.resTickets = resTickets;
	}

	public List<TechticketDTO> getTechTickets() {
		return techTickets;
	}

	public void setTechTickets(List<TechticketDTO> techTickets) {
		this.techTickets = techTickets;
	}

	public boolean isFromUser() {
		return fromUser;
	}

	public void setFromUser(boolean fromUser) {
		this.fromUser = fromUser;
	}

	public String[] getTicketFrom() {
		return ticketFrom;
	}

	public void setTicketFrom(String[] ticketFrom) {
		this.ticketFrom = ticketFrom;
	}

	public boolean isFromTech() {
		return fromTech;
	}

	public void setFromTech(boolean fromTech) {
		this.fromTech = fromTech;
	}

	public boolean isVar1() {
		return var1;
	}

	public void setVar1(boolean var1) {
		this.var1 = var1;
	}

	public boolean isVar2() {
		return var2;
	}

	public void setVar2(boolean var2) {
		this.var2 = var2;
	}

	public TechticketDTO getTechT() {
		return techT;
	}

	public void setTechT(TechticketDTO techT) {
		this.techT = techT;
	}

	public ResticketDTO getResT() {
		return resT;
	}

	public void setResT(ResticketDTO resT) {
		this.resT = resT;
	}

	public String getResponse() {
		return response;
	}

	public void setResponse(String response) {
		this.response = response;
	}

	public void goBack() {
		if (manager.getIdDomain() == 1) {
			techTickets = ttr.getAllTechtickets();
			resTickets = rtr.getAllRestickets();
		} else {
			techTickets = ttr.getTechticketsByUserDomain(manager);
			resTickets = rtr.getResticketsByUserDomain(manager);
		}
	}

	public void setTicketId(ActionEvent event) {
		response = "";
		int a = Integer.parseInt(event.getComponent().getAttributes()
				.get("ticketid").toString());
		if (event.getComponent().getAttributes().get("tickettype").toString()
				.equals("request")) {
			resT = rtr.getResticket(a);
			var1 = true;
			var2 = false;
		} else {
			techT = ttr.getTechticket(a);
			System.out.println(techT.getIdTechresource());
			var1 = false;
			var2 = true;
		}
		mpb.setPage("createResponse");
	}

	public void doResponse(ActionEvent event) {
		if (event.getComponent().getAttributes().get("tickettype").toString()
				.equals("request")) {
			ResponseDTO resp = new ResponseDTO();
			resp.setDescription(response);
			resp.setDate(new Date());
			resp.setIdUser(manager.getId());
			rr.insertResponse(resp);
			List<ResponseDTO> answers = rr.getAllResponsesByUserId(manager
					.getId());
			for (ResponseDTO r : answers) {
				if (r.getDescription().equals(resp.getDescription())) {
					resT.setIdResponse(r.getId());
					resT.setStatus("Processed");
					rtr.updateResticket(resT);
				}
			}
		} else {
			ResponseDTO resp = new ResponseDTO();
			resp.setDescription(response);
			resp.setDate(new Date());
			resp.setIdUser(manager.getId());
			rr.insertResponse(resp);
			List<ResponseDTO> answers = rr.getAllResponsesByUserId(manager
					.getId());
			for (ResponseDTO r : answers) {
				if (r.getDescription().equals(resp.getDescription())) {
					techT.setIdResponse(r.getId());
					techT.setStatus("Finalized");
					ttr.updateTechticket(techT);
				}
			}
		}
		mpb.setPage("manageRequest");
		goBack();
	}

	public void renderTables(ValueChangeEvent event) {
		if (manager.getIdDomain() == 1) {
			techTickets = ttr.getAllTechtickets();
			resTickets = rtr.getAllRestickets();
		} else {
			techTickets = ttr.getTechticketsByUserDomain(manager);
			resTickets = rtr.getResticketsByUserDomain(manager);
		}
		if (event.getNewValue().toString().equals("Basic user")) {
			fromUser = true;
			fromTech = false;
		} else if (event.getNewValue().toString().equals("Tech guy")) {
			fromUser = false;
			fromTech = true;
		} else {
			fromUser = false;
			fromTech = false;
		}
	}

	public UserDTO getUser(int id) {
		return ur.getUser(id);
	}

	public ResourceDTO getResource(int id) {
		return rdr.getResource(id);
	}

	public TechresourceDTO getTechresource(int id) {
		return trr.getTechresourcesById(id);
	}

	public DomainDTO getDomain(int id) {
		return dr.getDomain(id);
	}

	public MeasureunitDTO getMeasureunit(int id) {
		return mudr.getMeasureunit(id);
	}

}

